//
//  NSMutableAttributedString+KNPrice.m
//  Concubine
//
//  Created by ... on 16/7/12.
//  Copyright © 2016年 dengyun. All rights reserved.
//

#import "NSMutableAttributedString+KNPrice.h"

@implementation NSMutableAttributedString (KNPrice)

+ (NSMutableAttributedString *)currentPrice:(NSString *)currentPrice
                                originPrice:(NSString *)originPrice {
    NSString *cPrice = [NSString stringWithFormat:@"¥%@",currentPrice];
    NSString *oPrice = [NSString stringWithFormat:@"¥%@",originPrice];
    NSString *combinStr = [NSString stringWithFormat:@"%@  %@",cPrice, oPrice];
    NSMutableAttributedString *attributed = [[NSMutableAttributedString alloc] initWithString:combinStr];
    [attributed addAttributes:[self textFont:14.0] range:NSMakeRange(0, 1)];
    [attributed addAttributes:[self textFont:12.0] range:NSMakeRange(combinStr.length - oPrice.length, 1)];
    [attributed addAttributes:[self textFont:14.0] range:NSMakeRange(combinStr.length - oPrice.length + 1, originPrice.length)];
    NSDictionary *lineDic = @{NSForegroundColorAttributeName:[UIColor knLightGrayColor],
                              NSStrikethroughStyleAttributeName:@(NSUnderlinePatternSolid | NSUnderlineStyleSingle)};
    [attributed addAttributes:lineDic range:NSMakeRange(combinStr.length - oPrice.length, oPrice.length)];
    return attributed;
}

+ (NSDictionary *)textFont:(NSInteger)font {
    return @{NSFontAttributeName:[UIFont systemFontOfSize:font]};
}

+ (NSMutableAttributedString *)combinedStr:(NSString *)str price:(NSString *)price {
    NSString *defaultString = [NSString stringWithFormat:@"%@ ¥",str];
    NSString *att = [NSString stringWithFormat:@"%@%@",defaultString,price];
    NSDictionary *dic = @{NSForegroundColorAttributeName:[UIColor knBlackColor]};
    NSMutableAttributedString *attributed = [[NSMutableAttributedString alloc] initWithString:att];
    [attributed addAttributes:dic range:NSMakeRange(0, defaultString.length-1)];
    [attributed addAttributes:[self textFont:12.0] range:NSMakeRange(str.length, defaultString.length - str.length)];
    return attributed;
}

+ (NSMutableAttributedString *)combineOrderArrearagePrice:(NSString *)arrearagePrice {
    NSString *defaultString = @"欠款:  ¥";
    NSString *att = [NSString stringWithFormat:@"%@%@",defaultString,arrearagePrice];
    NSDictionary *dic = @{NSForegroundColorAttributeName:[UIColor knCountArrearageColor]};
    NSMutableAttributedString *attributed = [[NSMutableAttributedString alloc] initWithString:att];
    [attributed addAttributes:dic range:NSMakeRange(defaultString.length-1, arrearagePrice.length+1)];
    [attributed addAttributes:[self textFont:15.0] range:NSMakeRange(defaultString.length, arrearagePrice.length)];
    return attributed;
}

+ (NSMutableAttributedString *)combineOrderPrice:(NSString *)orderPrice
                                   shippingPrice:(NSString *)shippingPrice {
    NSString *defaultString = @"合计:  ¥";
    NSString *att = [NSString stringWithFormat:@"%@%@  (含运费¥%@)",defaultString,orderPrice,shippingPrice];
    NSDictionary *dic = @{NSForegroundColorAttributeName:[UIColor knThemeBlackColor]};
    NSMutableAttributedString *attributed = [[NSMutableAttributedString alloc] initWithString:att];
    [attributed addAttributes:dic range:NSMakeRange(defaultString.length-1, orderPrice.length+1)];
    [attributed addAttributes:[self textFont:15.0] range:NSMakeRange(defaultString.length, orderPrice.length)];
    return attributed;
}


+ (NSMutableAttributedString *)replaceFullyTimeStr:(NSString *)str {
    NSMutableAttributedString *attributed = [[NSMutableAttributedString alloc] initWithString:str];
    NSArray *array = [str componentsSeparatedByString:@"时"];
    NSString *hourStr = array[0];
    [attributed addAttributes:[self textFont:12.0] range:NSMakeRange(hourStr.length, 2)];
    [attributed addAttributes:[self textFont:12.0] range:NSMakeRange(hourStr.length + 5, 2)];
    [attributed addAttributes:[self textFont:12.0] range:NSMakeRange(str.length - 1, 1)];
    return attributed;
}

+ (NSMutableAttributedString *)price:(NSString *)price font:(NSInteger)font {
    NSString *att = [NSString stringWithFormat:@"¥%@",price];
    NSMutableAttributedString *attributed = [[NSMutableAttributedString alloc] initWithString:att];
    NSInteger textFont = 12.0;
    if (textFont > 14.0) {
        textFont = 14.0;
    }
    [attributed addAttributes:[self textFont:textFont] range:NSMakeRange(0, 1)];
    return attributed;
}

+ (NSMutableAttributedString *)underlineStr:(NSString *)str {
    if (str.length == 0 || [str isKindOfClass:[NSNull class]]) {
        return nil;
    }
    NSDictionary *attribtDic = @{NSUnderlineStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle]};
    NSMutableAttributedString *attribtStr = [[NSMutableAttributedString alloc]initWithString:str attributes:attribtDic];
    return attribtStr;
}

+ (NSMutableAttributedString *)solidLineStr:(NSString *)str {
    if (str.length == 0 || [str isKindOfClass:[NSNull class]]) {
        return nil;
    }
    NSDictionary *attribtDic = @{NSStrikethroughStyleAttributeName:@(NSUnderlinePatternSolid | NSUnderlineStyleSingle)};
    NSMutableAttributedString *attribtStr = [[NSMutableAttributedString alloc]initWithString:str attributes:attribtDic];
    return attribtStr;
}

+ (NSMutableAttributedString *)combineOrderInvoicePrice:(NSString *)totalPrice {
    NSString *defaultString = @"合计:  ¥";
    NSString *att = [NSString stringWithFormat:@"%@%@",defaultString,totalPrice];
    NSDictionary *dic = @{NSForegroundColorAttributeName:[UIColor knThemeBlackColor]};
    NSMutableAttributedString *attributed = [[NSMutableAttributedString alloc] initWithString:att];
    [attributed addAttributes:dic range:NSMakeRange(defaultString.length-1, totalPrice.length)];
    [attributed addAttributes:[self textFont:15.0] range:NSMakeRange(defaultString.length, totalPrice.length)];
    return attributed;
}

@end
